﻿using System.Data;
using System.Data.Common;

namespace XYAC.Framework.MonitorTireMaterialOnline.DataExporter
{
    //IDisposable接口:
    //当不再使用托管对象时，垃圾回收器会自动释放分配给该对象的内存；不过，进行垃圾回收的时间不可预知。
    //另外，垃圾回收器对窗口句柄或打开的文件和流等非托管资源一无所知。
    //******抽象类*******
    public abstract class clsComDB : IDisposable
    {
        private DbConnection db;
        //private DbDataAdapter adp;
        //private DataSet ds;
        private bool disposed = false;
        /// <summary>
        /// 返回数据库类型
        /// </summary>
        public virtual string DataBaseType
        {
            get
            {
                return "";
            }
        }
        /// <summary>
        /// 返回数据库连接状态
        /// </summary>
        public virtual bool CnState
        {
            get
            {
                return false;
            }
        }


        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public virtual bool Open()
        {
            return false;
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public virtual void Close()
        { }

        /// <summary>
        /// 执行一条SQL语句，返回受影响的行数
        /// </summary>
        /// <param name="SQL">Insert、Update、Delete等SQL语句</param>
        /// <returns></returns>
        public virtual int ExecSql(string SQL)
        {
            return 0;
        }

        /// <summary>
        /// 执行一条SQL语句，返回首行首列的值
        /// </summary>
        /// <param name="SQL">Select等SQL语句</param>
        /// <returns></returns>
        public virtual object GetFristValue(string SQL)
        {
            return null;
        }

        /// <summary>
        /// 执行一条SQL语句，返回一个数据适配器
        /// </summary>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public virtual DataAdapter GetAdapter(string SQL)
        {
            return null;
        }

        /// <summary>
        /// 执行得到一个dbCommand对象
        /// </summary>
        /// <returns></returns>
        public virtual DbCommand GetCommand()
        {
            return null;
        }

        /// <summary>
        /// 得到一个dbConnection对象
        /// </summary>
        /// <returns></returns>
        public virtual DbConnection GetConnection()
        {
            return null;
        }

        /// <summary>
        /// 得到一个DbDataReader
        /// </summary>
        /// <param name="SQL">SQL查询语句</param>
        /// <returns></returns>
        public virtual DbDataReader GetDataReader(string SQL)
        {
            return null;
        }

        /// <summary>
        /// 得到一个DataTable
        /// </summary>
        /// <param name="SQL">SQL查询语句</param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string SQL)
        {
            return null;
        }

        /// <summary>
        /// 返回数据集对象
        /// </summary>
        /// <param name="SQL">SQL查询语句</param>
        /// <returns></returns>
        public virtual DataSet GetDataSet(string SQL)
        {
            return null;
        }

        /// <summary> 
        ///释放资源. 
        /// </summary> 
        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary> 
        /// 释放对象实例变量. 
        /// </summary> 
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (db.State != ConnectionState.Closed)
                    {
                        db.Close();
                    }
                    db.Dispose();
                    db = null;
                }
            }
            disposed = true;
        }
    }
}
